//
//  ResourceModel.m
//  CareGiver
//
//  Created by Guillermo Moral on 8/23/14.
//  Copyright (c) 2014 Humana. All rights reserved.
//

#import "ResourceModel.h"

@implementation ResourceModel

-(id)initKey:(NSString*)key organization:(NSString*)organization address1:(NSString*)address1 address2:(NSString*)address2 latitude:(NSString*)latitude longitude:(NSString*)longitude; {
    
    self.key = key;
    self.organization = organization;
    self.address1 = address1;
    self.address2 = address2;
    self.longitude = longitude;
    self.latitude = latitude;
    self.status = [self updateStatus:@"Monday - Friday - 7:30 AM - 5:00 PM"];
    return self;
}

-(NSString*)updateStatus:(NSString*)status {
    
    NSString *ret;
    
    NSArray *statusArray = [status componentsSeparatedByCharactersInSet:
                        [NSCharacterSet characterSetWithCharactersInString:@"-"]
                        ];

    NSString *day1 = [statusArray objectAtIndex:0];
    day1 = [day1 stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    
    NSString* day2 = [statusArray objectAtIndex:1];
    day2 = [day1 stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    NSString* time1 = [statusArray objectAtIndex:2];
    NSString* time2 = [statusArray objectAtIndex:3];
    
    NSDateFormatter *df = [[NSDateFormatter alloc] init];

    [df setTimeZone:[NSTimeZone localTimeZone]];
    [df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    [df setDateFormat:@"hh:mm a"];
    
    // Current Time
    NSDate *currentTime = [NSDate date];
    NSString *currentTimeStr = [df stringFromDate:currentTime];
    NSDate *CT = [df dateFromString:currentTimeStr];
    
    // Start Time
    NSDate *ST = [df dateFromString:[NSString stringWithFormat:@"%@",time1]];
    
    // End Time
    NSDate *ET = [df dateFromString:[NSString stringWithFormat:@"%@",time2]];
    
    
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
   
    NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
    
    
    NSInteger currentWeekDay = [comps weekday];
    NSInteger startWeekDay = [self dayWithString:day1];
    NSInteger endWeekDay = [self dayWithString:day2];
    
    if(currentWeekDay>=startWeekDay && currentWeekDay <= endWeekDay) {
    
        if(CT>=ST) {
            if(CT<=ET) {
                ret = @"OPEN";
            } else {
                ret = @"CLOSED";
            }
        }
    }

    return ret;
}

-(NSInteger)dayWithString:(NSString*)day {

    if([day isEqualToString:@"Sunday"]) {
        return 1;
    } else if([day isEqualToString:@"Monday"]) {
        return 2;
    } else if([day isEqualToString:@"Tuesday"]) {
        return 3;
    } else if([day isEqualToString:@"Wednesday"]) {
        return 4;
    } else if([day isEqualToString:@"Thursday"]) {
        return 5;
    } else if([day isEqualToString:@"Friday"]) {
        return 6;
    }else if([day isEqualToString:@"Saturday"]) {
        return 7;
    } else {
        return 0;
    }
}

@end
